package sfeir.ergosum.adapter;

import java.util.ArrayList;

import sfeir.ergosum.R;
import sfeir.ergosum.model.Email;
import sfeir.ergosum.model.Person;
import sfeir.ergosum.model.Tel;
import sfeir.ergosum.tools.ThemeColor;
import android.content.Context;
import android.content.Intent;
import android.graphics.PorterDuff.Mode;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MailAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<Object> details;
    private Person p;
    private ViewHolder holder;

    public MailAdapter(Context c, Person p) {
        mContext = c;
        this.p = p;
        mInflater = LayoutInflater.from(mContext);
        details = new ArrayList<Object>();
        populate();
    }

    public void update(Person newP) {
        p = newP;
        details.clear();
        populate();
        notifyDataSetChanged();
    }

    private void populate() {
        for (Email t : p.getEmails())
            details.add(t);
        for (Tel t : p.getTels())
            details.add(t);
    }

    private static class ViewHolder {
        ImageView logo;
        TextView info;
    }

    @Override
    public int getCount() {
        return details.size();
    }

    @Override
    public Object getItem(int position) {
        return details.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);

            holder = new ViewHolder();
            holder.logo = (ImageView) convertView
                    .findViewById(R.id.list_item_image);
            holder.info = (TextView) convertView
                    .findViewById(R.id.list_item_content);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Object tu = getItem(position);

        if (tu instanceof Email) {
            Email t = (Email) tu;

            final String email = t.getEmail();
            final String[] mailto = { email };
            OnClickListener emailListener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent sendIntent = new Intent(Intent.ACTION_SEND);
                    sendIntent.setData(Uri.parse("mailto:" + mailto));
                    sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
                    sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                    sendIntent.setType("text/plain");
                    mContext.startActivity(Intent.createChooser(sendIntent,
                            mContext.getResources().getString(R.string.mail_client)));
                }
            };

            holder.info.setText(email);
            convertView.setClickable(true);
            convertView.setOnClickListener(emailListener);

            switch (t.getTypeEmail()) {

            case Email.PERSONAL_EMAIL:
                holder.logo.setImageResource(R.drawable.email_perso);
                break;
            case Email.PROFESSIONNAL_EMAIL:
                holder.logo.setImageResource(R.drawable.email_pro);
                break;
            case Email.UNKNOWN:
                holder.logo.setImageResource(R.drawable.email);
                break;
            default:
                holder.logo.setImageResource(R.drawable.email);
                break;
            }
        }

        else if (tu instanceof Tel) {
            final String tel = (((Tel) tu).getPhoneNumber()).replace(" ", "");

            OnClickListener telListener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent iSms = new Intent(Intent.ACTION_VIEW, Uri
                            .parse("sms:" + tel));
                    mContext.startActivity(iSms);
                }
            };

            holder.info.setText(tel);
            convertView.setClickable(true);
            convertView.setOnClickListener(telListener);

            switch (((Tel) tu).getType()) {

            case Tel.MOBILE_LINE:
                holder.logo.setImageResource(R.drawable.sms_icon);
                break;
            case Tel.HOME_LANDLINE:
                holder.logo.setImageResource(R.drawable.sms_perso);
                break;
            case Tel.PROFESSIONNAL_LINE:
                holder.logo.setImageResource(R.drawable.sms_pro);
                break;
            default:
                holder.logo.setImageResource(R.drawable.sms_icon);
                break;
            }
        }
        ThemeColor theme = new ThemeColor(mContext);
        convertView.getBackground().setColorFilter(theme.getCellColors().get(1), Mode.SRC);
        convertView.getBackground().setAlpha(200);
        return convertView;
    }
}
